package cn.kgc.rbac.exception;

import cn.kgc.rbac.common.CodeMsg;
import cn.kgc.rbac.common.ResponseResult;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author: mosin
 * @version: v1.0  2023/1/30
 */
@RestControllerAdvice
public class GlobalException {

    // 用户名错误
    @ExceptionHandler(UnknownAccountException.class)
    public ResponseResult  unknownAccountException(UnknownAccountException e){
        e.printStackTrace();
        return new ResponseResult(CodeMsg.USER_NOT_EXIST);
    }

    // 密码错误
    @ExceptionHandler(IncorrectCredentialsException.class)
    public ResponseResult  incorrectCredentialsException(IncorrectCredentialsException e){
        e.printStackTrace();
        return new ResponseResult(CodeMsg.USER_PASSWORD_ERROR);
    }

    // 用户被禁用
    @ExceptionHandler(LockedAccountException.class)
    public ResponseResult  lockedAccountException(LockedAccountException e){
        e.printStackTrace();
        return new ResponseResult(CodeMsg.USER_LOCKED);
    }

    // 权限异常处理 AuthorizationException

    @ExceptionHandler(AuthorizationException.class)
    public ResponseResult authorizationException(AuthorizationException e){
        e.printStackTrace();
        return  new ResponseResult(CodeMsg.USER_NO_AUTHORIZATION);
    }

    // 其它异常
    @ExceptionHandler(Exception.class)
    public ResponseResult  OtherException(Exception e){
        e.printStackTrace();
        return new ResponseResult(CodeMsg.FAIL);
    }


}
